import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/widget/my_button.dart';

class DeviceNameEditController extends GetxController {
  final TextEditingController textController = TextEditingController();
  final RxBool hasText = false.obs;

  @override
  void onInit() {
    super.onInit();
    textController.addListener(() {
      hasText.value = textController.text.isNotEmpty;
    });
  }

  @override
  void onClose() {
    textController.dispose();
    super.onClose();
  }

  void saveDeviceName() {
    if (textController.text.isNotEmpty) {
      // 这里添加保存设备名称的逻辑
      Get.back(result: textController.text);
    }
  }

  void cancel() {
    Get.back();
  }
}

class DeviceNameEditPage extends StatelessWidget {
  final String? initialName;

  const DeviceNameEditPage({Key? key, this.initialName}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final DeviceNameEditController controller =
        Get.put(DeviceNameEditController());

    if (initialName != null && initialName!.isNotEmpty) {
      controller.textController.text = initialName!;
      controller.hasText.value = true;
    }

    return Scaffold(
      backgroundColor: Colors.grey[100],
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        centerTitle: true,
        title: Text(
          '设备名',
          style: TextStyle(
            color: Colors.black,
            fontSize: 17.sp,
            fontWeight: FontWeight.w500,
          ),
        ),
        leading: TextButton(
          onPressed: controller.cancel,
          child: Text(
            '取消',
            style: TextStyle(
              color: Colors.black87,
              fontSize: 16.sp,
            ),
          ),
        ),
        actions: [
          Obx(() => Center(
                child: Container(
                  width: 79.w,
                  height: 32.h,
                  padding: EdgeInsets.only(right: 18.w),
                  child: MyButton(
                    padding: EdgeInsets.zero,
                    onPressed: controller.hasText.value
                        ? controller.saveDeviceName
                        : null,
                    text: '保存',
                    backgroundColor: controller.hasText.value
                        ? Colours.app_main
                        : Colors.blue.withOpacity(0.5),
                    fontSize: 14.sp,
                    radius: 12.r,
                  ),
                ),
              )),
        ],
      ),
      body: Column(
        children: [
          SizedBox(height: 11.h),
          Container(
            color: Colors.white,
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: TextField(
              controller: controller.textController,
              autofocus: true,
              style: TextStyle(
                fontSize: 16.sp,
                color: Colors.black,
              ),
              decoration: InputDecoration(
                border: InputBorder.none,
                hintText: '请输入设备名称',
                hintStyle: TextStyle(
                  color: Colors.grey,
                  fontSize: 16.sp,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
